
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  poolint.pull.tablename;

import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import dbmanager.DBManager;

import basicprocess.authentication.Condition;

//import pool.bean.Td_property_details_Bean;
import pool.bean.Transaction_details_Bean;
import query.query.ParseQuery;

public class Transaction_details extends IntegrateSubPool
{
	
	String tid;
	private String poolTablename="transaction_details";
	private String reflectSchemaProperty = "td_property_details";
	
	private Hashtable<Object,Object> transHash = new Hashtable<Object, Object>();	
	private Vector<Object> transPdVector = new Vector<Object>();
	
	public boolean insert(Hashtable<Object,Object> poolHash,Hashtable<Object,Object>moduleHashtable)
	{	
		productTableHash.putAll(poolHash);
		ModuleHash.putAll(moduleHashtable);
		
		setTransDetailsVector();
		
		Hashtable<Object,Object> temptransHash = new Hashtable<Object, Object>();
		Vector<Object> transDetailsVector = new Vector<Object>();
		
		isTransactionAvailable();
		tid=tidValue;
		transHash.put("tid",tidValue);
	
		temptransHash.put(transHash,transPdVector);
		transDetailsVector.add(temptransHash);
	
		@SuppressWarnings("unused")
		String tempStr; // If object not found in Transaction than it will get error, and it will insert into database.
		try
		{
			tempStr = isTransIdAvailable( columnNameValue );
			return true;
		}
		catch (Exception e)
		{
			try 
			{
				request=poolTablename;
				
				/**Setting request parameters like process, user name, password, etc.*/
				setRequestParameter();
				
				/** Inserting transaction & Property...**/
				System.out.println(" Integration::transaction:: Insert Process... ");
				Transaction_details_Bean trans = new Transaction_details_Bean();
				trans.setReqParam(req);
				trans.setId(tid);
				trans.setProcess(process);
				trans.setPropertyVector(transDetailsVector);
				trans.setTable(poolTablename);
				
				trans.insert();
				return true;
			} 
			catch (Exception e1)
			{
				e1.printStackTrace();
			}
			return false;
		}
	}
	
	private void setTransDetailsVector()
	{	
		Hashtable<Object,Object> tempHash = new Hashtable<Object, Object>();		
		tempHash.putAll(ModuleHash);		
		try 
		{	
			Enumeration<Object> enumt;
			enumt= tempHash.keys();
			
			while(enumt.hasMoreElements())
			{
				String key = enumt.nextElement().toString();
				String field = tempHash.get(key).toString();
				transHash.put(field,productTableHash.get(key));										
			}		
		} catch (Exception e){ }			
	}

	@SuppressWarnings("unchecked")
	public boolean update(Hashtable<Object, Object> productHashtable,Hashtable<Object, Object> moduleHashtable)
	{
		productTableHash.putAll(productHashtable);
		ModuleHash.putAll(moduleHashtable);
		
		setTransDetailsVector();	
		
		//request=reflectSchemaProperty;
		request=poolTablename;
		setRequestParameter();
		isTransactionAvailable();
		tid=tidValue;
		Hashtable<Object,Object> oldHash = new Hashtable<Object, Object>();
		oldHash.put("tid",tid);
		oldHash.put("item",transHash.get("item"));
		Vector<Object> oldVector = new Vector<Object>();
		oldVector.add(oldHash);
		Vector<Object> newVector = new Vector<Object>();
		transHash.put("tid",tid);
		newVector.add(transHash);
		
 		/** Inserting transaction & Property...**/
		System.out.println(" Integration::transaction:: Update Process... ");
		
		Transaction_details_Bean transDet = new Transaction_details_Bean();
		transDet.setReqParam(req);
		transDet.setProcess(process);		
		transDet.setTable(poolTablename);	
		transDet.setNewPropertyVector(newVector);
		transDet.setOldPropertyVector(oldVector);		
		transDet.update();
		return true;
	}

	/*public void generateProductHash() {
		// TODO Auto-generated method stub
		
	}
*/

	
	/*public boolean delete(Hashtable<Object, Object> productTableHash, Hashtable<Object, Object> moduleHash) 
	{		
		System.out.println(" Integration::transaction:: Delete Process... ");
		
		setRequestParameter();
		
		Hashtable<Object,Object> oldHash = new Hashtable<Object, Object>();
		oldHash.put("tid",oldcolumnNameValue);
		//oldHash.put("item",transHash.get("item"));
		
		Vector<Object> oldVector = new Vector<Object>();
		oldVector.add(oldHash);
				
		Transaction_details_Bean transDet = new Transaction_details_Bean();
		transDet.setDBManager(dbsql);
		transDet.setReqParam(req);
		transDet.setProcess(process);		
		transDet.setTable(poolTablename);	
		//transDet.setNewPropertyVector(newVector);
		transDet.setOldPropertyVector(oldVector);		
		transDet.delete();
		
		return true;	
	}*/
	
	
	
	public boolean delete(Hashtable<Object, Object> productTableHash, Hashtable<Object, Object> moduleHash) 
	{		
		System.out.println(" Integration::transaction:: Delete Process... ");
		
		setRequestParameter();
		
		Enumeration< Object > enumEle = oldColumnNameValue.elements();
	
		Vector<Object> oldVector = new Vector<Object>();
		while( enumEle.hasMoreElements() )
		{
			Hashtable< Object, Object > oldHash = new Hashtable< Object, Object > ();
			String colVal = enumEle.nextElement().toString();
			oldHash.put( "tid", colVal );
			oldVector.add(oldHash);
		}
		
		//Hashtable<Object,Object> oldHash = new Hashtable<Object, Object>();
		//oldHash.put("tid",oldcolumnNameValue);
		//oldHash.put("item",transHash.get("item"));
		
		
		
		/*Vector<Object> newVector = new Vector<Object>();
		newVector.add(transHash);*/
		
 		/** Inserting transaction & Property...**/
		
		
		Transaction_details_Bean transDet = new Transaction_details_Bean();
		transDet.setReqParam(req);
		transDet.setProcess(process);		
		transDet.setTable(poolTablename);	
		//transDet.setNewPropertyVector(newVector);
		transDet.setOldPropertyVector(oldVector);		
		transDet.delete();
		
		return true;	
	
	}
	
		
	/*private String isTransIdAvailable(String columnNameValue) throws Exception
	{
		String tableValue;
		tableValue = tableName;
		String value = gin.getId(columnNameValue);
<<<<<<< .mine
		String query = "SELECT p.tdid FROM " + reflectSchemaProperty + " p JOIN " + reflectSchemaProperty + " p1 ";
		query = query + "ON (p.tdid = p1.tdid) WHERE p.pid = " + gin.getId("TYPE") + " AND p.pv = " + gin.getId(tableValue) + " AND p1.pid = " + gin.getId(columnName) + "  AND ";
=======
		String query = "SELECT p.tdid FROM " + reflectSchemaProperty + " p JOIN " + reflectSchemaProperty + " p1 ";
		query = query + "ON (p.tdid = p1.tdid) WHERE p.pid = " + gin.getId("TYPE") + " AND p.pv = " + tableValue + " AND p1.pid = " + gin.getId(columnName) + " AND ";
>>>>>>> .r2932
		if(!value.equals("0"))
		{				
			query = query + " p1.pv = " + value;
		}
		else
		{
			query = query + " p1.vt = '" + columnNameValue + "'";
		}
		ResultSet temp = dbsql.getSelect(query);
		temp.next();
<<<<<<< .mine
		return temp.getString("tdid");
	}*/
	
	
	private String isTransIdAvailable( Hashtable< Object, Object > columnNameValue ) throws Exception
	{
		ParseQuery query = new ParseQuery();
		query.SetCondition(columnNameValue);
		query.SetTableName("transaction_details");
		Hashtable<Object, Object> fieldHash = new Hashtable<Object, Object>();
		fieldHash.put("field", "id");		
		query.SetFieldList(fieldHash);
		query.processQuery();
		String[] tdid = query.getbuffer();
		return tdid[1];		
		//return temp.getString("tdid");
	}
		
	
	
	/*public boolean isParentAvailable()
	{		
		boolean flag=false;
		//String LHSid = gin.getId("LHS");
		ResultSet rs = getJoinResultSet();
		try 
		{
			rs.next();
			String id=rs.getString(1);
			flag=false;
		}
		catch (Exception e)
		{
			try 
			{
				String fieldname=gin.getId(getFieldName());
				String  query;
				String value=gin.getId(columnValue);
				if(!value.equals("0"))
				{				
					query="select tdid from "+reflectSchemaProperty+" where pid="+fieldname+" and pv="+value;
				}
				else
				{
					query="select tdid from "+reflectSchemaProperty+" where pid="+fieldname+" and vt="+columnValue;
				}
				rs= dbsql.getSelect(query);
				rs.next();
				String id=rs.getString(1);
				flag= true;
			} 
			catch (Exception e1)
			{
				flag= false;
			}					
		}
		return flag;
	}*/
	
	
	public boolean isParentAvailable()
	{		
		boolean flag=false;
		//String LHSid = gin.getId("LHS");
		/*String JoinId = gin.getId("join");
		String TableId = gin.getId("TABLE");
		String Tablenameid = gin.getId("TABLENAME");
		String TypeId = gin.getId("TYPE");		
		
		//String joinTableObj="";
		String query = "select p3.mid from property_details p3 JOIN (property_details p2 JOIN property_details p1 ON"
			+" (p2.pv=p1.mid)) ON (p3.mid=p2.mid) WHERE p3.pid='"+TypeId+"' AND p3.pv='"+JoinId+"' AND p2.pid='"+TableId+"' AND "
			+" p1.pid='"+Tablenameid+"' AND p1.pv='"+gin.getId(tableName)+"'";
		
		ResultSet rs= dbsql.getSelect(query);*/
		ResultSet rs = getJoinResultSet();
		try 
		{
			rs.next();
			String id=rs.getString(1);
			flag=false;
			rs.close();
		}
		catch (Exception e)
		{
			try 
			{
				ParseQuery query = new ParseQuery();
				query.SetCondition(columnNameValue);
				query.SetTableName("transaction_details");
				Hashtable<Object, Object> fieldHash = new Hashtable<Object, Object>();
				fieldHash.put("field", "id");		
				query.SetFieldList(fieldHash);
				query.processQuery();
				String[] id = query.getbuffer();
				if(id.length >= 1)
					flag= true;		
				
				/*String fieldname=gin.getId( getFieldName() );
				String  query;
				String condition = new String();
				
				Enumeration< Object > enumVal = columnNameValue.elements();
				
				while( enumVal.hasMoreElements() )
				{
					String colVal = enumVal.nextElement().toString();
					String value = gin.getId(colVal);
					
					if( !value.equals( "0" ) )
					{				
						condition +=  " pid = " + fieldname + " and pv = " + value;
					}
					else
					{
						condition += " pid = " + fieldname + " and vt = " + value;					
					}
				}
				
				query =  "select tdid from " + reflectSchemaProperty + " where " + condition;  
				//String value=gin.getId(columnValue);
				//if(!value.equals("0"))
				//{				
					//query="select tdid from "+reflectSchemaProperty+" where pid="+fieldname+" and pv="+value;
				//}
				//else
				//{
					//query="select tdid from "+reflectSchemaProperty+" where pid="+fieldname+" and vt="+columnValue;
				//}
				rs= DBManager.getSelect(query);
				rs.next();
				String id=rs.getString(1);
				flag= true;
				rs.close();*/
			}
			catch (Exception e1)
			{
				flag= false;
			}					
		}
		return flag;
	}
}
	
